package com.dhz.wlzs.app.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dhz.wlzs.api.entity.BoxInfo;
import com.dhz.wlzs.api.service.BoxInfoService;
import com.github.pagehelper.PageHelper;


/**
 * box_info：通信模块box信息
 * @author heyw
 * @since 2018-03-19 17:11:39
 * @copyright: hyw Copyright (c) 2017-2020 All Rights Reserved.
 */
 
@RestController
@RequestMapping("/BoxInfo")
public class BoxInfoController  {

    @Autowired
    private BoxInfoService boxInfoService;
    
    @RequestMapping(value="/view/{id}/{method}", method = RequestMethod.GET)
    public BoxInfo view(@PathVariable(value = "id") final Long id  ,@PathVariable(value = "method")String method){
        BoxInfo bean= boxInfoService.findById(id);
        return bean ;
    }
    
    @RequestMapping(value="/list")
    public List<BoxInfo> list(HttpServletRequest req,@RequestParam Map<String, Object> map ,@RequestParam(required = false, defaultValue = "1") int pageNo,@RequestParam(required = false, defaultValue = "10") int pageRows, Model model){
        PageHelper.startPage(pageNo, pageRows);
        List<BoxInfo> list=boxInfoService.findAll(map);
		return list;
    }
    
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public ModelAndView toAdd(HttpServletRequest req,@ModelAttribute("bean") BoxInfo bean){
        return new ModelAndView("/BoxInfo/create");
    }
    
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public ModelAndView save(HttpServletRequest req,@Validated @ModelAttribute("bean") BoxInfo bean,BindingResult result){
        if(result.hasErrors()){
            return new ModelAndView("/BoxInfo/create");
        }
        return new ModelAndView("redirect:/BoxInfo/list","flag",boxInfoService.save(bean));
    }
    
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public ModelAndView update(@Valid BoxInfo bean){
        return new ModelAndView("redirect:/BoxInfo/list","flag",boxInfoService.update(bean)>0);
    }
    
    //注意:数据更新操作一般必须是post请求, method = RequestMethod.POST
    @RequestMapping(value="/del/{id}")
    public ModelAndView delete(@PathVariable(value = "id") final Long id ){
        return new ModelAndView("redirect:/BoxInfo/list","flag",boxInfoService.deleteById(id)>0);
    }
   
}
